package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.constant.RedisConstant;
import cn.itcast.domain.SetMeal;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.SetMealService;
import cn.itcast.utils.QiniuUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/meal")
public class SetMealController {

    @Reference
    private SetMealService setMealService;

    @Reference
    private JedisPool jedisPool;

    /**
     * 上传 img 图片
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @RequestMapping("/upload")
    public Result uploadData(@RequestParam("imgFile")MultipartFile multipartFile) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename(); // 获取原始文件名 为了获取后缀a5e8e729-74ce-4939-bf36-9cdc02fb2ae51.jpg
        int lastIndex = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(lastIndex - 1);
        String fileName = UUID.randomUUID().toString() + suffix;

        try {
            QiniuUtils.upload2Qiniu(multipartFile.getBytes(),fileName);
             //添加一个集合
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }


    }

    /**
     *  分页查询
     * @param pageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult getPageResult(@RequestBody QueryPageBean pageBean){

        PageResult result = setMealService.getMealPage(pageBean);

        return result;
    }

    @RequestMapping("/addMeal")
    public Result addMealData(Integer[] checkgroupIds ,@RequestBody SetMeal setMeal){

        try {
            setMealService.addData(checkgroupIds,setMeal);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/deletemeal")
    public Result deletemeal(Integer id){
        try {
            setMealService.deleteSetMeal(id);
            return new Result(true,"delete SetMeal data success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"delete SetMeal data fail");
        }
    }
}
